package com.durablemethods.futures.server;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class TraderPositionServlet extends HttpServlet{

	private static final long serialVersionUID = -7938663553660927953L;
	private StaticTraderPositionService positionService = new StaticTraderPositionService();
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		List<TraderPositionSummary> summaries = positionService.getPositionSummaries();
		List<String> teams = positionService.getTeams();
		
		StringBuilder header = new StringBuilder();
		header.append("Trader,");
		for (int i = 0; i < 16; i++) {
			header.append(teams.get(i));
			if (i<15){
				header.append(",");
			}
		}
		resp.getWriter().println(header.toString());
		for (TraderPositionSummary traderPositionSummary : summaries) {
			StringBuilder builder = new StringBuilder();
			builder.append(traderPositionSummary.getTrader()).append(",");
			for (int i = 0; i < 16; i++) {
				String string = teams.get(i);
				Integer position = traderPositionSummary.getPosition(string);
				builder.append(position.toString());
				if (i<15){
					builder.append(",");
				}
			}
			resp.getWriter().println(builder.toString());
		}
	}

}
